package com.tom.blog.controller;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.tom.blog.entity.UserFile;
import com.tom.blog.service.UserFileService;
import com.tom.blog.util.MD5Util;

@CrossOrigin(origins = "*", maxAge = 3600) // 允许所有域名访问
@Controller
public class UserFileController {

	@Autowired
	private UserFileService fileService;

	@RequestMapping(value = "/")
	public String index(Model model) {
		model.addAttribute("files", fileService.listFiles());
		return "index";
	}

	/**
	 * 获取文件片信息
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@ResponseBody
	public ResponseEntity<Object> forDownload(@PathVariable String id) {

		UserFile file = fileService.get(id);

		if (file != null) {
			return ResponseEntity.ok()
					.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=\"" + file.getName() + "\"")
					.header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
					.header(HttpHeaders.CONTENT_LENGTH, file.getSize() + "").header("Connection", "close")
					.body(file.getContent());
		} else {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
		}

	}

	/**
	 * 在线显示文件
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/view/{id}")
	@ResponseBody
	public ResponseEntity<Object> forViewOnline(@PathVariable String id) {

		UserFile file = fileService.get(id);

		if (file != null) {
			return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "fileName=\"" + file.getName() + "\"")
					.header(HttpHeaders.CONTENT_TYPE, file.getContentType())
					.header(HttpHeaders.CONTENT_LENGTH, file.getSize() + "").header("Connection", "close")
					.body(file.getContent());
		} else {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
		}

	}

	/**
	 * 上传
	 * 
	 * @param file
	 * @param redirectAttributes
	 * @return
	 */
	@PostMapping("/")
	public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {

		try {
			UserFile f = new UserFile(file.getOriginalFilename(), file.getContentType(), file.getSize(),
					file.getBytes());
			f.setMd5(MD5Util.md5(file.getInputStream()));
			fileService.save(f);
		} catch (IOException | NoSuchAlgorithmException ex) {
			ex.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "Your " + file.getOriginalFilename() + " is wrong!");
			return "redirect:/";
		}

		redirectAttributes.addFlashAttribute("message",
				"uploaded " + file.getOriginalFilename() + " successfully!");

		return "redirect:/";
	}

	@PostMapping("/upload")
	@ResponseBody
	public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
		UserFile returnFile = null;
		try {
			UserFile f = new UserFile(file.getOriginalFilename(), file.getContentType(), file.getSize(),
					file.getBytes());
			f.setMd5(MD5Util.md5(file.getInputStream()));
			returnFile = fileService.save(f);
			returnFile.setPath("http://localhost:8081/view/" + f.getId());
			returnFile.setContent(null);
			return ResponseEntity.status(HttpStatus.OK).body("http://localhost:8081/view/" + f.getId());

		} catch (IOException | NoSuchAlgorithmException ex) {
			ex.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
		}

	}
}
